<?php

/**
 * created by zzy
 * date: 2018/1/26 14:07
 */

namespace App\Api\Service\Buyers\Controllers;

use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;
use App\Eloquent\Ygt\BuyersAddress;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class AddressController extends Controller
{
    public $userId          = 25;
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $buyersId           = request('buyers_id',0);
        if( !$userInfo || !$buyersId )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
//        $where              = ['company_id'=>$companyId,'buyers_id'=>$buyersId];
        $where              = ['buyers_id'=>$buyersId];
        $columns            = 'id,consignee,phone,province_id,city_id,area_id,';
        $columns            .= 'address,province_name,city_name,area_name,is_default';
        $limit              = '';
        $offset             = '';
        $orderby            = ['is_default','desc'];
        $collection         = BuyersAddress::getList($where,$columns,$limit,$offset,$orderby);
        $lists              = $collection->toArray();
        return $lists;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        $buyersId           = request('buyers_id',0);
        if( !$userInfo || !$buyersId)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $isSubmit           = request('is_submit',0);
        if($isSubmit){
            $data                   = request()->all();
            $provinceName = $cityName = $areaName = '';
            if(isset($data['province_id']))
            {
                $provinceId     = $data['province_id'];
                $where          = ['provinceID'=>$provinceId];
                $collection     = Province::where($where)->first();
                $provinceName   = $collection->province;
            }
            if(isset($data['city_id']))
            {
                $cityId         = $data['city_id'];
                $where          = ['cityID'=>$cityId];
                $collection     = City::where($where)->first();
                $cityName       = $collection->city;
            }
            if(isset($data['area_id']))
            {
                $areaId         = $data['area_id'];
                $where          = ['areaID'=>$areaId];
                $collection     = Area::where($where)->first();
                $areaName       = $collection->area;
            }
            $data['province_name']  = $provinceName;
            $data['city_name']      = $cityName;
            $data['area_name']      = $areaName;
            $data['company_id']     = $companyId;
            $fieldList              = BuyersAddress::$tableFieldList;
            $tableData              = BuyersAddress::setDataByTableField($data,$fieldList);
            $tableDataCheckResult   = BuyersAddress::checkDataByTableField($tableData,$fieldList);
            if($tableDataCheckResult !== true)
            {
                xThrow(ERR_PARAMETER, $tableDataCheckResult);
            }
            $editResult             = BuyersAddress::edit($tableData,'id');
            if($editResult===false)
            {
                xThrow(ERR_UNKNOWN);
            }
            $message            = $id ? '编辑成功' : '添加成功';
            return ['message'=>$message];
        } else {
            $result                 = [];
            if($id)
            {
                $where              = ['id'=>$id];
                $info               = BuyersAddress::getInfo($where);
                if($info)
                {
                    $result         = $info->toArray();
                }
            }
            return $result;
        }
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if( !$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'id'=>$id];
        $result             = BuyersAddress::del($where);
        if($result === false)
        {
            xThrow(ERR_UNKNOWN);
        }
        return ['message'=>'删除成功'];
    }
}